<?php
/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/11/20
 * Time: 03:12
 */

namespace App\Http\Services;


use App\Http\Models\Category;
use App\Http\Models\Mark;
use App\Http\Models\User;
use App\Http\Models\Work;
use Symfony\Component\Finder\Exception\AccessDeniedException;

class WorkService
{
    public static function getList($limit, $offset, $order = 'desc', $option = [])
    {
        $query = Work::where('is_del', 0);
        if (!empty($option)) {
            foreach($option as $key => $value) {
                if ($key == 'score') {
                    if ($value == 'equal') {
                        $query = $query->where('score', 0);
                    }
                    if ($value == 'greater') {
                        $query = $query->where('score', '>', 0);
                    }
                } else {
                    $query = $query->where($key, $value);
                }
            }
        }
        $total = $query->count();
        $works = $query->orderBy('id', $order)->limit($limit)->offset($offset)->get();
        $data = [];
        foreach ($works as $work) {
            $user = User::where('id', $work->user_id)->first();
            $category = Category::where('id', $work->category_id)->first();
            $data[] = [
                'id' => $work->id,
                'title' => $work->title,
                'user_id' => $work->user_id,
                'realname' => $user->realname,
                'category_id' => $work->category_id,
                'category_name' => $category->name,
                'pics' => explode(';', $work->content),
                'score' => $work->score,
                'created_at' => date('Y-m-d', strtotime($work->created_at))
            ];
        }

        return [
            'total' => $total,
            'rows' => $data,
            'page_size' => $limit,
            'offset' => $offset,
            'pages' => ceil($total / $limit),
            'page_number' => $offset / $limit + 1
        ];
    }

    public static function getDetail($id)
    {
        $work = Work::where('id', $id)->where('is_del', 0)->first();
        if (!$work) {
            throw new AccessDeniedException('作品不存在或已删除', 4032);
        }
        $user = User::where('id', $work->user_id)->first();
        $category = Category::where('id', $work->category_id)->first();
        $marks = Mark::where('work_id', $id)->where('is_del', 0)->get();
        $scoreList = [];
        $totalScore = 0;
        foreach ($marks as $mark) {
            $teacher = User::where('id', $mark->teacher_id)->first();
            $scoreList[] = [
                'teacher' => [
                    'id' => $teacher->id,
                    'realname' => $teacher->realname
                ],
                'score' => $mark->score
            ];
            $totalScore += $mark->score;
        }
        return [
            'id' => $work->id,
            'title' => $work->title,
            'desc' => $work->desc,
            'user_id' => $work->user_id,
            'realname' => $user->realname,
            'category_id' => $work->category_id,
            'category_name' => $category->name,
            'pics' => explode(';', $work->content),
            'score' => $work->score,
            'score_list' => $scoreList,
            'total_score' => $totalScore,
            'created_at' => date('Y-m-d', strtotime($work->created_at))
        ];
    }

    public static function getWorkMarkList($limit, $offset, $sort = null)
    {
        $query = Work::where('is_del', 0);
        $total = $query->count();
        $query = $query->limit($limit)->offset($offset);
        if ($sort) {
            $query = $query->orderBy('score', $sort);
        } else {
            $query = $query->orderBy('id', 'desc');
        }
        $works = $query->get();
        $data = [];
        foreach ($works as $work) {
            $user = User::where('id', $work->user_id)->first();
            $category = Category::where('id', $work->category_id)->first();
            $marks = Mark::where('work_id', $work->id)->where('is_del', 0)->get();
            $scoreList = [];
            foreach ($marks as $mark) {
                $teacher = User::where('id', $mark->teacher_id)->first();
                $scoreList[] = [
                    'teacher' => [
                        'id' => $teacher->id,
                        'realname' => $teacher->realname
                    ],
                    'score' => $mark->score
                ];
            }
            $data[] = [
                'id' => $work->id,
                'title' => $work->title,
                'user_id' => $work->user_id,
                'realname' => $user->realname,
                'category_id' => $work->category_id,
                'category_name' => $category->name,
                'score' => $work->score,
                'score_list' => $scoreList,
                'created_at' => date('Y-m-d', strtotime($work->created_at))
            ];
        }

        return [
            'total' => $total,
            'rows' => $data,
            'page_size' => $limit,
            'offset' => $offset,
            'pages' => ceil($total / $limit),
            'page_number' => $offset / $limit + 1
        ];
    }

    public static function getAllWorkMark()
    {
        $works = Work::where('is_del', 0)->orderBy('id', 'desc')->get();
        $data = [];
        foreach ($works as $work) {
            $user = User::where('id', $work->user_id)->first();
            $marks = Mark::where('work_id', $work->id)->where('is_del', 0)->get();
            $scoreList = '';
            foreach ($marks as $mark) {
                $teacher = User::where('id', $mark->teacher_id)->first();
                $scoreList .= '专家:'.$teacher->realname.' 分数:'.$mark->score.' ';
            }
            $data[] = [
                $work->id,
                $user->realname,
                $work->title,
                $scoreList,
                $work->score
            ];
        }

        return $data;
    }

    public static function save($data)
    {
        $now = date('Y-m-d H:i:s', time());
        if (array_key_exists('id', $data)) {
            $work = Work::where('id', $data['id'])->where('is_del', 0)->first();
            if (!$work) {
                throw new AccessDeniedException('作品不存在或已删除', 4032);
            }
            if ($work->status == 0) {
                throw new AccessDeniedException('作品已评审,不可更新', 4032);
            }
        } else {
            $work = new Work();
            $work->status = 1;
            $work->is_del = 0;
            $work->created_at = $now;
        }

        foreach ($data as $key => $value) {
            $work->$key = $value;
        }
        $work->updated_at = $now;
        $work->save();
    }

    public static function delete($id)
    {
        $work = Work::where('id', $id)->where('is_del', 0)->first();
        if (!$work) {
            throw new AccessDeniedException('作品不存在或已删除', 4032);
        }
//        if ($work->status == 0) {
//            throw new AccessDeniedException('作品已评审,不可更新', 4032);
//        }
        $work->is_del = 1;
        $work->updated_at = date('Y-m-d H:i:s', time());
        $work->save();
    }
}